#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/8/17 11:57
# @Author  : youfeng
import time
from datetime import datetime


class Util(object):
    @staticmethod
    def time_to_timestamp(time_str):
        # string转化结构化时间
        time_array = time.strptime(time_str, "%Y-%m-%d %H:%M:%S")
        # 结构化时间转时间戳
        timestamp = time.mktime(time_array)
        return int(timestamp)

    @staticmethod
    def timestamp_to_time(timestamp):
        time_local = time.localtime(timestamp)
        return time.strftime("%Y-%m-%d %H:%M:%S", time_local)

    @staticmethod
    def get_current_date(fmt="%Y-%m-%d"):
        return datetime.now().strftime(fmt)

    @staticmethod
    def get_future_date(days, fmt="%Y-%m-%d"):
        import datetime
        one_day = datetime.timedelta(days=days)
        day = datetime.datetime.now() + one_day
        return day.strftime(fmt)

    @staticmethod
    def get_current_time(fmt="%Y-%m-%d %H:%M:%S"):
        return datetime.now().strftime(fmt)

    @staticmethod
    def get_ip_from_proxy(proxy):
        if not isinstance(proxy, basestring):
            return None
        if "@" in proxy:
            ip = proxy.split("@")[1]
        else:
            ip = proxy.split("//")[1]

        if ":" in ip:
            return ip.split(":")[0]
        return ip

    @staticmethod
    def get_ip_port_from_proxy(proxy):
        if not isinstance(proxy, basestring):
            return None
        if "@" in proxy:
            ip = proxy.split("@")[1]
        else:
            ip = proxy.split("//")[1]

        return ip
